#include "chooselevelscene.h"

ChooseLevelScene::ChooseLevelScene(QWidget *parent) : QMainWindow(parent)
{

    this->setWindowTitle("coin");
    this->setFixedSize(QSize(320, 588));
    QMenuBar *bar = menuBar();
    this->setMenuBar(bar);
    QMenu *menu = bar->addMenu("begin");
    QAction *quitMenu = menu->addAction("quit");

    connect(quitMenu, &QAction::triggered, this, [=](){
        this->close();
    });

    // 准备音乐
    QSound* btnChoiceSound = new QSound(":/TapButtonSound.wav");
    QSound* btnBackSound = new QSound(":/BackButtonSound.wav");

    // 返回按键
    MyPushButton* back = new MyPushButton(":/BackButton.png", ":/BackButtonSelected.png");
    back->move(this->width() - back->width() - 10, this->height() - back->height() - 10);
    back->setParent(this);
    back->show();

    connect(back, &MyPushButton::clicked, this, [=](){
        qDebug()<<"back back...";
        btnBackSound->play();
        this->close();
        parent->show();
    });

    playScene = NULL;

    // 创建关口选择按钮
    for(int i = 0; i < 20; i++){
        MyPushButton* btn = new MyPushButton(":/LevelIcon.png", ":/LevelIcon_select.png");
        btn->setParent(this);
        btn->move(25+(i%4)*70,130+(i/4)*70);
        btn->show();

        QLabel *level = new QLabel(this);
        level->setText(QString::number(i+1));
        level->setFixedSize(btn->width(),btn->height());
        level->move(btn->x(), btn->y());
        level->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        level->setAttribute(Qt::WA_TransparentForMouseEvents);

        connect(btn, &QPushButton::clicked, this, [=](){
            // 播放音乐
            btnChoiceSound->play();
            // 点击事件
            qDebug()<<"game level clicked";
            // 跳转到游戏舞台
            playScene = new PlayScene(i+1);

            // 接收play退出信号
            connect(playScene, &PlayScene::quitPlay, this, [=](){
                qDebug()<<"signed quit";
                playScene->close();
                this->show();
            });

            QTimer::singleShot(300, this, [=](){

                this->hide();
                playScene->show();

            });

        });

    }

}

void ChooseLevelScene::paintEvent(QPaintEvent* event){
    QPainter painter(this);
    QPixmap pixmap;
    pixmap.load(":/OtherSceneBg.png");
    painter.drawPixmap(0,0,this->width(), this->height(), pixmap);

    pixmap.load(":/Title.png");
    painter.drawPixmap(20,30, pixmap);
}
